package demogeneric;

/**
 * 写一个泛型类，求一个泛型数组当中的最大值
 * 将来传入的 T 一定是实现了 Comparable 接口的
 */
class Alg<T extends Comparable<T>> {
    public T findMax(T[] array) {
        T max = array[0];
        for (int i = 1; i < array.length; i++) {
            if (max.compareTo(array[i]) < 0) {
                max = array[i];
            }
        }
        return max;
    }
}

class Student implements Comparable<Student> {
    @Override
    public int compareTo(Student o) {
        return 0;
    }
}

class Alg2 {
    //泛型方法
    public<T extends Comparable<T>> T findMax(T[] array) {
        T max = array[0];
        for (int i = 1; i < array.length; i++) {
            if (max.compareTo(array[i]) < 0) {
                max = array[i];
            }
        }
        return max;
    }
}

class Alg3 {
    //泛型方法
    public static <T extends Comparable<T>> T findMax(T[] array) {
        T max = array[0];
        for (int i = 1; i < array.length; i++) {
            if (max.compareTo(array[i]) < 0) {
                max = array[i];
            }
        }
        return max;
    }
}

public class Test2 {
    public static void main(String[] args) {
        Integer[] array = {1, 2, 3, 5, 67, 88, 99};
        Integer max = Alg3./*<Integer>*/findMax(array);
        System.out.println(max);
    }

    public static void main2(String[] args) {
        Integer[] array = {1, 2, 3, 5, 67, 88};
        Alg2 alg2 = new Alg2();
        Integer max = alg2.findMax(array);
        System.out.println(max);
    }

    public static void main1(String[] args) {
        Integer[] array = {1, 2, 3, 5, 67};
        Alg<Integer> alg = new Alg<>();
        Integer max = alg.findMax(array);
        System.out.println(max);
    }
}
